<?php

//供vue脚手架测试的跨域请求API接口数据
header("Access-Control-Allow-Origin: *"); // 允许任何域名进行访问
header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); // 允许的HTTP方法
header("Access-Control-Allow-Headers: X-Requested-With"); // 允许的HTTP请求头

function external($url)
{
    $apiUrl = $url; // 替换为你的API URL
    $ch = curl_init($apiUrl);

    // 设置cURL选项
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); // 如果API需要JSON请求体

    // 发起请求
    $response = curl_exec($ch);

    // 检查是否有错误发生
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
    }

    // 关闭cURL资源
    curl_close($ch);

    // 处理响应
    return $response;
    // $result = json_decode($response, true); // 将响应解码为数组
    // print_r($result);
}

// 下面是你的PHP代码
//发起内部API调用
if ($_GET['select'] == "self") {
    $data = ["beijing" => "晴转多云", "nanning" => "晴"];
    $data = json_encode($data);
    echo $data;
    exit();
}

switch ($_GET['select']) {
    case "Jinan":
        $city = "101120101";
        break;
    case "Beijing":
        $city = "101010100";
        break;
    case "Shenzhen":
        $city = "101280601";
        break;
    case "Nanjing":
        $city = "101190101";
        break;
    case "Guangzhou":
        $city = "101280101";
        break;
    case "Chengdu":
        $city = "101270101";
        break;
    case "Shanghai":
        $city = "101020100";
        break;
    default:
        $city = "101120101";
}

//发起外部API调用
$data = external("http://t.weather.itboy.net/api/weather/city/" . $city);
echo $data;
